from django import template
from django import forms
from django.http import HttpResponseRedirect
import datetime
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from tagging.models import Tag, TaggedItem
from pirate_reputation.models import ReputationManager, Reputation, ReputationDimension, ReputationEvent
from pirate_core import HttpRedirectException, namespace_get, FormMixin, template_for_model

from customtags.decorators import block_decorator
register = template.Library()
block = block_decorator(register)

get_namespace = namespace_get('pp_reputation')


@block
def pp_get_reputation_events_graph(context, nodelist, *args, **kwargs):
    '''
    This block tag can create or process forms to get tags.
    Usage is as follows:
        
    {% pp_get_reputation_events_graph user=request.object x=8 y=100 %}
       Do stuff with {{ pp_reputation.graph_html }}
    {% endpp_get_reputation %}
    
    This template tag dynamically generates the html required for a (x,y) graph 
    where x is the activity rate and y is the time dimension. This graph shows
    users a basic idea of the user's activity rate.

    '''
    
    context.push()
    namespace = get_namespace(context)

    user = kwargs.get('user',None)
    x = kwargs.get('x', None)
    y = kwargs.get('y', None)
    
    

@block
def pp_get_reputation_events(context, nodelist, *args, **kwargs):
    '''
    This block tag can create or process forms to get tags.
    Usage is as follows:
        
    {% pp_get_reputation_events user=request.object %}
       Do stuff with {{ pp_reputation.reputation_events }}.
    {% endpp_get_reputation %}

    '''
    
    context.push()
    namespace = get_namespace(context)

    user = kwargs.get('user',None)

    if user is not None and isinstance(user, User):
        #get argument score
        scores = {}
        tot_score = 0
        rep = ReputationEvent.objects.filter(initiator=user)
    else: 
        rep = []
    
    namespace['reputation_events'] = rep

    output = nodelist.render(context)
    context.pop()
    return output   

@block
def pp_get_reputation(context, nodelist, *args, **kwargs):
    '''
    This block tag can create or process forms to get tags.
    Usage is as follows:
        
    {% pp_get_reputation user=request.object %}
       Do stuff with {{ pp_reputation.reputation }}.
    {% endpp_get_reputation %}

    '''
    
    context.push()
    namespace = get_namespace(context)

    user = kwargs.get('user',None)

    if user is not None and isinstance(user, User):
        #get argument score
        scores = {}
        tot_score = 0
        for dim in ReputationDimension.objects.all():
            rep = Reputation.objects.get_user_score(user, dim)
            try: 
                scores[str(dim)]=rep.score
                tot_score+=rep.score
            except: pass #rep does not yet exist
    else: 
        scores ={}
        tot_score = 0
    
    namespace['reputation_keys'] = scores.items()
    namespace['reputation'] = tot_score

    output = nodelist.render(context)
    context.pop()
    return output   
